import wxCharts from '../../../utils/wxcharts.js';
const db = wx.cloud.database();
const _ = db.command;
var app = getApp();
var lineChart = null;
var startPos = null;
Page({
  data: {
    hidden: 0
  },
  touchHandler: function (e) {
    lineChart.scrollStart(e);
  },
  moveHandler: function (e) {
    lineChart.scroll(e);
  },
  touchEndHandler: function (e) {
    lineChart.scrollEnd(e);
    lineChart.showToolTip(e, {
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },

  //数据传入的地方：
  //categories 是横坐标， data是纵坐标
  createSimulationData: function (e) {
    // console.log(e);
    var categories = [];
    var data = [];
    for (var i = 0; i < e.length; i++) {
      // categories.push('2020,01,-' + (i + 1));
      // data.push(Math.random() * (20 - 10) + 10);
      categories.push(e[i].date),
      data.push(e[i].running)
    }
    // console.log(categories, data)
    return {
      categories: categories,
      data: data
    }


    // })



  },
  onLoad: function (e) {
    wx.getStorage({
      key: 'userInfo',
      success: res => {
        // console.log(res.data);
        this.setData({
          Info: res.data
        })
      }
    })
    var windowWidth = 1000;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }
    // 放到实际应用时要加where字句， 判断某个人的openid
    db.collection('runing')
      .where({
        "openid": "oHj774mjHx78VE8kDo9qku-UfGeY"
      })
      .get()
      .then(res => {
        this.setData({
          dataInfo: res.data[0].running
        })
        // console.log(this.data.dataInfo);
        var simulationData = this.createSimulationData(this.data.dataInfo);
        console.log(simulationData);
        lineChart = new wxCharts({
          canvasId: 'lineCanvas',
          type: 'line',
          categories: simulationData.categories,
          animation: false,
          series: [{
            name: '步数统计',
            data: simulationData.data,
            format: function (val, name) {
              return val.toFixed(2);
            }
          }],
          xAxis: {
            disableGrid: false
          },
          yAxis: {
            // title: '成交金额 (万元)',
            format: function (val) {
              return val.toFixed(2);
            },
            min: 0
          },
          width: windowWidth,
          height: 200,
          dataLabel: true,
          dataPointShape: true,
          enableScroll: true,
          extra: {
            lineStyle: 'curve'
          }
        });
      })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})